<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <!--        <el-form-item label="点检计划" label-width="78px">-->
        <!--          <el-select-->
        <!--            v-model="searchParam.planId"-->
        <!--            placeholder="请选择点检计划"-->
        <!--            clearable-->
        <!--          >-->
        <!--            <el-option-->
        <!--              v-for="item in planList"-->
        <!--              :key="item.id"-->
        <!--              :label="item.planName"-->
        <!--              :value="item.id"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="工单编号" label-width="78px">
          <el-input
            v-model="searchParam.orderNo"
            placeholder="请输入工单编号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="作业时间" label-width="78px">
          <el-date-picker
            v-model="searchParam.workTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getworkTime"
          />
        </el-form-item>
        <!--        <el-form-item label="作业类型">-->
        <!--          <el-select-->
        <!--            v-model="searchParam.sopTypeId"-->
        <!--            placeholder="请选择作业类型"-->
        <!--            clearable-->
        <!--            :disabled="sopType !== null"-->
        <!--          >-->
        <!--            <el-option-->
        <!--              v-for="item in typeList"-->
        <!--              :key="item.id"-->
        <!--              :label="item.sopTypeName"-->
        <!--              :value="item.id"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <!--        <el-form-item label="资产编号" label-width="78px">-->
        <!--          <el-input-->
        <!--            v-model="searchParam.assetCode"-->
        <!--            placeholder="请输入资产编号"-->
        <!--            clearable-->
        <!--            @keyup.enter="search"-->
        <!--          />-->
        <!--        </el-form-item>-->
        <el-form-item label="工单状态" label-width="78px">
          <el-select
            v-model="searchParam.orderStatus"
            placeholder="请选择工单状态"
            clearable
          >
            <el-option
              v-for="item in useDictionary('QmsCheckOrderStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="执行人" label-width="78px">
          <el-input
            v-model="searchParam.executePersonName"
            placeholder="请输入执行人"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!--        <el-table-column-->
        <!--          label="点检计划"-->
        <!--          prop="planName"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--          align="center"-->
        <!--        />-->
        <el-table-column
          label="工单编号"
          prop="orderNo"
          min-width="150"
          show-overflow-tooltip
        />
        <el-table-column
          label="作业时间"
          prop="workTime"
          min-width="170"
          show-overflow-tooltip
          align="center"
        />
        <!--        <el-table-column-->
        <!--          label="作业类型名称"-->
        <!--          prop="sopTypeName"-->
        <!--          min-width="130"-->
        <!--          show-overflow-tooltip-->
        <!--          align="center"-->
        <!--        />-->
        <!--        <el-table-column-->
        <!--          label="标准作业编号"-->
        <!--          prop="sopStandardCode"-->
        <!--          min-width="130"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <!--        <el-table-column-->
        <!--          label="标准作业名称"-->
        <!--          prop="sopStandardName"-->
        <!--          min-width="250"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <!--        <el-table-column-->
        <!--          label="资产编号"-->
        <!--          prop="assetCode"-->
        <!--          min-width="200"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <!--        <el-table-column-->
        <!--          label="资产名称"-->
        <!--          prop="assetName"-->
        <!--          min-width="150"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <el-table-column
          label="工单状态"
          prop="orderStatus"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("QmsCheckOrderStatus", scope.row.orderStatus) }}
          </template>
        </el-table-column>
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="200"
          show-overflow-tooltip
        />
        <el-table-column
          label="执行人"
          prop="executePersonName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <!--        <el-table-column-->
        <!--          label="复核人"-->
        <!--          prop="checkPersonName"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--          align="center"-->
        <!--        />-->
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="170"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="100" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-search />
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <qmspointcheckorder-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useDictionary, useTable } from "@/utils/useTable";
import request from "@/utils/request";
import { getStandardTypesApi } from "@/utils/commonData";

defineOptions({
  name: "QMSQMSPOINTCHECKORDER",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/qms/qmspointcheckorder/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  assetKind: "",
  orderNo: "",
  workTimeSearch: [],
  workTimeStart: "",
  workTimeEnd: "",
  sopTypeId: "",
  sopTypeCode: "",
  assetId: "",
  assetCode: "",
  orderStatus: "",
  executePersonId: "",
};

const deleteParam = {
  api: "/qms/qmspointcheckorder/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const sopType = ref();

const reset = () => {
  searchParam.value.orderNo = "";
  searchParam.value.workTimeSearch = [];
  searchParam.value.assetCode = "";
  searchParam.value.orderStatus = "";
  searchParam.value.executePersonName = "";
  searchParam.value.workTimeEnd = "";
  searchParam.value.workTimeStart = "";
  searchParam.value.planId = "";
  getTableList();
};

onMounted(async () => {
  // await getTypeList("");
  // await getPlanList();
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  const { data } = await request({
    url: `/qms/qmspointcheckorder/get/${id}`,
    method: "get",
  });
  addDialog.data = data;
}

/** 打开资产点检工单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "资产点检工单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  addDialog.visible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getworkTime(data: any) {
  if (data) {
    searchParam.value.workTimeStart = data[0];
    searchParam.value.workTimeEnd = data[1];
  } else {
    searchParam.value.workTimeStart = "";
    searchParam.value.workTimeEnd = "";
  }
}

// 获取作业类型列表
// const typeList = ref(<any>[]);
//
// async function getTypeList(value: string) {
//   const params = {
//     taskKind: value,
//     assetKind: "4",
//   };
//   typeList.value = (await getStandardTypesApi(params)) ?? [];
// }

// 获取点检计划列表
// const planList = ref(<any>[]);

// async function getPlanList() {
//   const { data } = await request({
//     url: "/qms/equPrecisionCheckPlan/getlist",
//     method: "post",
//     data: {},
//   });
//   planList.value = data ?? [];
// }
</script>
